using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Blog.IRepository;
using Blog.Model;
using SqlSugar;
using SqlSugar.IOC;

namespace Blog.Repository
{
    public class BaseRepository<T> : SimpleClient<T>, IBaseRepository<T> where T : class, new()
    {
        public BaseRepository(ISqlSugarClient context=null):base(context)
        {
            base.Context = DbScoped.Sugar;

            // 创建数据库
            base.Context.DbMaintenance.CreateDatabase();

            // 创建表
            base.Context.CodeFirst.InitTables(
                typeof(BlogNews),
                typeof(TypeInfo),
                typeof(WriterInfo)
            );
        }

        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<bool> CreateAsync(T t)
        {
            return await base.InsertAsync(t);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            return await base.DeleteByIdAsync(id);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<bool> EditAsync(T t)
        {
            return await UpdateAsync(t);
        }

        /// <summary>
        /// 导航查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> FindAsync(int id)
        {
            return await base.GetByIdAsync(id);
        }

        /// <summary>
        /// 条件导航查询
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public async Task<T> FindAsync(Expression<Func<T, bool>> func)
        {
            return await base.GetSingleAsync(func);
        }

        /// <summary>
        /// 查询全部数据
        /// </summary>
        /// <returns></returns>
        public virtual async Task<List<T>> QueryAsync()
        {
            return await base.GetListAsync();
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public virtual async Task<List<T>> QueryAsync(Expression<Func<T, bool>> func)
        {
            return await base.GetListAsync(func);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <returns></returns>
        public virtual async Task<List<T>> QueryAsync(int page, int size, global::SqlSugar.RefAsync<int> total)
        {
            return await base.Context.Queryable<T>()
            .ToPageListAsync(page, size, total);
        }

        /// <summary>
        /// 分页条件查询
        /// </summary>
        /// <returns></returns>
        public virtual async Task<List<T>> QueryAsync(Expression<Func<T, bool>> func, int page, int size, global::SqlSugar.RefAsync<int> total)
        {
            return await base.Context.Queryable<T>()
            .Where(func)
            .ToPageListAsync(page, size, total);
        }
    }
}